package com.huawei.esdk.uc.sametime;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.sametime.services.UCLoginService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.widgets.CallComeInPopup;
import com.huawei.esdk.uc.sametime.widgets.EsdkUCConvWindow;
import com.ibm.collaboration.realtime.im.community.CommunityLifecycleEvent;
import com.ibm.collaboration.realtime.im.community.CommunityListener;
import com.ibm.collaboration.realtime.im.community.CommunityLoginEvent;
import com.ibm.collaboration.realtime.im.community.CommunityServiceEvent;
import com.ibm.collaboration.realtime.im.community.CommunityStatusEvent;

public class SametimeCommunitylistener implements CommunityListener
{

    private static Logger logger = Logger
            .getLogger(SametimeCommunitylistener.class);

    public SametimeCommunitylistener()
    {
        logger.debug("sametime communitylistener inject");
    }

    @Override
    public void handleCommunityLifecycleEvent(CommunityLifecycleEvent arg0)
    {
        logger.debug("handleCommunityLifecycleEvent , type is: "
                + arg0.getType());
    }

    @Override
    public void handleCommunityLoginEvent(CommunityLoginEvent arg0)
    {
        if (CommunityLoginEvent.TYPE_LOGGED_IN == arg0.getType())
        {
            logger.debug("sametime login event, type is: TYPE_LOGGED_IN");
            GlobalConfiguration.getInstance().setSamtimeLogin(true);

            if (!GlobalConfiguration.getInstance().isUCLogin())
            {
                UCLoginService.startLoginUC();
            }

        }
        else if (CommunityLoginEvent.TYPE_LOGGED_OUT == arg0.getType()
                || CommunityLoginEvent.TYPE_PREPARE_TO_LOGOUT == arg0.getType())
        {
            logger.debug("sametime logout event, type is: TYPE_LOGGED_OUT");
            GlobalConfiguration.getInstance().setSamtimeLogin(false);

            Display.getDefault().syncExec(new RealeaseCallResource());

            if (GlobalConfiguration.getInstance().isUCLogin())
            {
                UCLoginService.startLogoutUC();
            }
        }
        else
        {
            logger.debug("sametime login/logout event, type is: "
                    + arg0.getType());
            GlobalConfiguration.getInstance().setSamtimeLogin(false);
        }
    }

    @Override
    public void handleCommunityServiceEvent(CommunityServiceEvent arg0)
    {
        logger
                .debug("handleCommunityServiceEvent , type is: "
                        + arg0.getType());
    }

    @Override
    public void handleCommunityStatusEvent(CommunityStatusEvent arg0)
    {
        logger.debug("handleCommunityStatusEvent , type is: " + arg0.getType()
                + ", Status is :" + arg0.getStatus());
    }

    private static class RealeaseCallResource implements Runnable
    {
        @Override
        public void run()
        {
            EsdkUCConvWindow.getInstance().endCall();
            CallComeInPopup.getInstance().closeComeInPopup();
        }
    }
}
